<template>
    <div class="balance_detail">
        <van-nav-bar :title="msg == 1 ? '我的积分' : msg == 2 ? '授信额度' : msg == 3?'余额明细': msg == 4?'站长明细': '商家花朵'" left-arrow fixed @click-left="onClickLeft" :class="{ scrolled: isScrolled }">
            <template #title>
                {{ msg == 1 ? '我的积分' : msg == 2 ? '授信额度' : msg == 3?'余额明细': msg == 4?'站长余额明细': '商家花朵' }}
                <img style="width: .36rem;vertical-align: middle;margin-top: -.08rem;"
                    src="../../images/mine/wh_icon.png" alt="" v-if="incomeType.introduce" @click="showPopover=!showPopover">
                <div class="tips" v-if="showPopover">
                    {{ incomeType.introduce }}
                </div>
            </template>
        </van-nav-bar>
        <div class="balance_detail_top" @click="showPopover=false" v-if="incomeType.extra_show!=1">
            <div class="balance_left">{{ incomeType.balance }}</div>
            <div class="balance_right">{{ incomeType.label }}</div>
        </div>
        <div class="balance_detail_top extra" @click="showPopover=false" v-else>
            <div class="extra_left">
                <div class="balance_left">{{incomeType.balance }}</div>
                <div class="balance_right">{{ incomeType.label }}</div>
            </div>
            <div class="extra_right">
                <div class="balance_left">{{incomeType.extra_value }}</div>
                <div class="balance_right">{{ incomeType.extra_label }}</div>
            </div>
        </div>
        <div class="balance_detail_content"  @click="showPopover=false">
            <!--  -->
            <div class="balance_detail_content_day" @click="dayShow = true">{{ days }}<van-icon name="arrow-down"
                    style="color: #999;" /></div>
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
                <div class="balance_list" v-for="(item, index) in fansList" :key="index">
                    <div class="balance_list_left">
                        <div>{{ item.remark }}</div>
                        <div>{{ item.create_time }}</div>
                    </div>
                    <div class="balance_list_right" :style="{ color: item.income == true ? '#55A624' : '#F32A23' }">
                        {{ item.income == true ? '+' : '-' }}{{ item.amount }}</div>
                </div>
            </van-list>
        </div>

        <van-popup v-model="dayShow" position="bottom" :style="{ height: '40%' }">
            <van-datetime-picker :value="new Date(currentDate)" type="date" title="选择年月日" :min-date="minDate"
                :max-date="maxDate" @confirm="onConfirmDay" @cancel="onCancelDay" />
        </van-popup>
    </div>
</template>
<script>
import { parseTime } from '@/tool/methods'
import {
    INCOME_STATISTICS,
    LOG_INCOME_LIST
} from "@/api/mine";
import {formatSubsidy} from '@/tool/methods'
export default {
    name: 'balance',
    data() {
        return {
            days: "本月",
            dayShow: false,
            minDate: new Date(1930, 0, 1),
            maxDate: new Date(2999, 10, 1),
            currentDate: new Date(),
            loading: false,
            finished: false,
            notifyList: [],
            msg: null,
            incomeType: {},
            fansList: [],
            page: 1,
            year: '',
            month: '',
            day: '',
            pageSize: 20,
            date_range: 1,
            showPopover: false,
            actions: [{ text: '' }],
            isScrolled:false
        }
    },
    created() {

    },
    mounted() {
        this.msg = this.$route.query.id
        document.title = this.$route.query.id == 1 ? '我的积分' : this.$route.query.id == 2 ? '授信额度':  this.$route.query.id == 3?'余额明细' :this.$route.query.id == 4?'站长余额明细': '商家花朵'
        this.getIncome()
        window.addEventListener("scroll", this.handleScroll);
    },
    beforeDestroy() {
        window.removeEventListener("scroll", this.handleScroll);

    },
    methods: {
        handleScroll() {
            // console.log(window.scrollY);

            this.isScrolled = window.scrollY > 26;
        },
        getIncome() {
            this.$http.get(INCOME_STATISTICS + (this.msg == 1 ? 1 : this.msg == 2 ? 20:this.msg == 4 ? 12 : 10)).then(res => {
                this.incomeType = res.data
                this.incomeType.balance = formatSubsidy(this.incomeType.balance)
                this.incomeType.extra_value = formatSubsidy(this.incomeType.extra_value)
                this.actions = [{ text: res.data.introduce }]
            })
        },
        onClickLeft() {
            this.$router.back()
        },
        onConfirmDay(val) {
            this.currentDate = parseTime(val, '{y}-{m}-{d}')
            this.days = this.currentDate
            this.year = this.currentDate.split('-')[0]
            this.month = this.currentDate.split('-')[1]
            this.day = this.currentDate.split('-')[2]
            this.date_range = 0
            this.dayShow = false
            this.page = 1
            this.fansList = []
            this.finished = false
            this.loading = true;
            this.onLoad()
        },
        onCancelDay() {
            this.dayShow = false
            // this.days = null
            this.days = '本月'
            this.year = ''
            this.month = ''
            this.day = ''
            this.page = 1
            this.fansList = []
            this.date_range = 1
            this.finished = false
            this.loading = true;
            this.onLoad()
        },
        onLoad() {
            let params = {};
            params["page_num"] = this.page;
            params["page_size"] = this.pageSize;
            params['type'] = this.msg == 1 ? 1 : this.msg == 2 ? 20:this.msg == 4 ?12 : 10;
            params['income_type'] = null;
            params['date_range'] = this.date_range
            params['year'] = this.year;
            params['month'] = this.month;
            params['day'] = this.day;
            // params['status'] = this.active;
            this.$http.get(LOG_INCOME_LIST, {
                params: params
            }).then((res) => {
                const {
                    list
                } = res.data;
                this.total_count = res.data.pages.total_count;
                if (list && list.length) {
                    this.page++;
                    this.fansList = this.fansList.concat(list);

                    // 加载状态结束
                    this.loading = false;
                    if (list.length < this.pageSize) {
                        this.finished = true;
                    }
                } else {
                    this.finished = true;
                }
            });
        }
    }
}
</script>
<style lang="scss" scoped>
.balance_detail {
    padding-top: 46px;
    background-color: #F2F4F5;
    height: calc(100vh - 46px);

    ::v-deep .van-hairline--bottom::after {
        border: none;
    }

    ::v-deep .van-icon {
        color: #fff;
    }

    ::v-deep .van-nav-bar__title {
        width:150px;
        color: #fff;
    }

    ::v-deep .van-nav-bar__arrow {
        font-size: 20px;
    }

    ::v-deep .van-nav-bar__text {
        color: #fff;
    }

    ::v-deep .van-nav-bar {
        background: transparent;
        position: fixed; // 改为固定定位
        top: 0; // 添加这行
        left: 0; // 添加这行
        right: 0; // 添加这行
        z-index: 1000; // 添加这行，确保它在其他元素之上

        // position: fixed;
        &.scrolled {
            background-color: #ffffff; // 或者你想要的任何颜色

            .van-nav-bar__title,
            .van-icon,
            .van-nav-bar__text {
                color: #333; // 滚动后变为黑色
            }
        }
    }

    .balance_detail_top {
        width: 100%;
        height: 4.72rem;
        background: url('../../images/mine/img_beij4.png') no-repeat center;
        position: relative;
        /* 相对定位 */
        top: -46px;
        /* 根据你的 NavBar 高度调整，Vant UI 默认 NavBar 高度为 46px */
        width: 100%;
        background-size: cover;
        color: #fff;
        text-align: center;
        padding-top: 1.5rem;
        box-sizing: border-box;

        .balance_left {
            font-family: PingFangSC, PingFang SC;
            font-weight: 500;
            font-size: .6rem;
            color: #FFFFFF;
            line-height: .84rem;
            // text-align: right;
            font-style: normal
        }

        .balance_right {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: .32rem;
            color: #FFFFFF;
            line-height: .44rem;
            // text-align: right;
            font-style: normal;
            margin-top: .12rem;
        }
    }
    .extra{
        // width: 80%;
        // margin: 0 auto;
        display: flex;
        justify-content: space-around;
        // align-items: center;
        .extra_left{
            width: 40%;
        }
        .extra_right{
            width: 40%;
        }
    }

    .balance_detail_content {
        width: 95%;
        height: calc(100vh - 4rem);
        margin: 0 auto;
        border-radius: .2rem;
        overflow: auto;
        // border-top-right-radius: .2rem;
        background: #fff;
        padding-top: .4rem;
        box-sizing: border-box;
        margin-top: -1.9rem;
        position: relative;
        z-index: 1;
        padding: .2rem;
        box-sizing: border-box;

        .balance_detail_content_day {
            min-width: 1.2rem;
            max-width: 2rem;
            border-radius: .36rem;
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: .24rem;
            color: #666666;
            padding: .08rem 0;
            box-sizing: border-box;
            text-align: center;
            font-style: normal;
            border: 1px solid #8A8A8A;

            ::v-deep .van-icon {
                color: #666;
            }
        }

        .balance_list {
            margin-top: .2rem;
            padding-bottom: .2rem;
            border-bottom: 1px solid #E5E5E5;
            display: flex;
            justify-content: space-between;

            .balance_list_left {
                font-family: PingFangSC, PingFang SC;
                font-weight: 400;
                font-size: .28rem;
                color: #333333;
                line-height: .4rem;
                // text-align: left;
                font-style: normal;

                div:last-child {
                    font-family: PingFangSC, PingFang SC;
                    font-weight: 400;
                    font-size: .24rem;
                    color: #999999;
                    line-height: .34rem;
                    // text-align: left;
                    font-style: normal;
                    margin-top: .12rem;

                }
            }

            .balance_list_right {
                font-family: PingFangSC, PingFang SC;
                font-weight: 500;
                font-size: .32rem;
                color: #55A624;
                line-height: .44rem;
                // text-align: center;
                font-style: normal;
            }
        }

    }
}

.tips {
    width: 50%;
    min-height: auto;
    max-height: 2rem;
    overflow: auto;
    position: absolute;
    left: 30%;
    top: 90%;
    background: #333;
    white-space: wrap;
    border-radius: .1rem;
    text-align: left;
    padding: .1rem;
    font-size: .28rem;
    font-weight: 400;
}
.tips::after {
    content: '';
    position: absolute;
    left: 50%; /* 使三角形居中 */
    bottom: 100%; /* 放置在 tips 的上方 */
    margin-left: -0.1rem; /* 调整位置，使三角形居中 */
    border-width: 0.1rem; /* 设置三角形的大小 */
    border-style: solid;
    border-color: transparent transparent #333 transparent; /* 三角形颜色 */
}
</style>